package foo.bar.wiki.web.action.page;

import foo.bar.wiki.web.action.BaseActionBean;
import foo.bar.wiki.web.extension.PageTypeConverter;
import foo.bar.wiki.services.WikiService;
import foo.bar.wiki.services.UserService;
import foo.bar.wiki.services.exception.BusinessLogicExecution;
import foo.bar.wiki.domain.Page;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.SimpleMessage;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.Validate;

/**
 * @author tmjee
 * @version $Date$ $Id$
 */
public class AddToFavouriteActionBean extends BaseActionBean {

    private WikiService wikiService;
    private UserService userService;



    @Validate(converter = PageTypeConverter.class)
    private Page page;
    

    public Resolution save() throws Throwable {
        return handleBusinessLogicException(new BusinessLogicExecution() {
            public Resolution execute() throws Throwable {
                userService.addPageToUserFavourite(getCurrentUser(), page);
                getContext().getMessages().add(new SimpleMessage("Page ["+page.getName()+"] being successfully added to your favourite page list"));
                return new RedirectResolution(PageActionBean.class, "view").addParameter("page", page.getId());
            }
        });
    }



    @SpringBean("wikiService")
    public void setWikiService(WikiService wikiService) {
        this.wikiService = wikiService;
    }


    @SpringBean("userService")
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public Page getPage() {
        return page;
    }

    public void setPage(Page page) {
        this.page = page;
    }
}
